package com.proton.carepatchtemp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapterholder.ShareHistoryHolder;
import com.proton.carepatchtemp.database.table.ShareHistory;
import com.proton.carepatchtemp.utils.net.DeviceCenter;

import java.util.List;

/**
 * Created by MoonlightSW on 2016/12/20.
 */

public class ShareHistoryAdapter extends RecyclerView.Adapter<ShareHistoryHolder> {

    private List<ShareHistory.SharelistBean> mShareHistories;
    private LayoutInflater mInflater;
    private long profileId = 0;

    public ShareHistoryAdapter(Context context, List<ShareHistory.SharelistBean> list, long profileId) {
        this.mShareHistories = list;
        this.profileId = profileId;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public ShareHistoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_share_history, parent, false);
        ShareHistoryHolder historyHolder = new ShareHistoryHolder(view);
        return historyHolder;
    }

    @Override
    public void onBindViewHolder(ShareHistoryHolder holder, int position) {
        ShareHistory.SharelistBean temp = mShareHistories.get(position);
        holder.mTv_share_phone_number.setText(temp.getShareto());
        holder.mIv_delete_share_item.setOnClickListener(v -> {
            DeviceCenter.cancleShare(temp.getId(), profileId);
            mShareHistories.remove(temp);
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return null == mShareHistories ? 0 : mShareHistories.size();
    }
}
